package com.group2.view;

import model.Student;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.util.Objects;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import model.*;
import static model.netBase.IP;
public class StudentShowFrame extends  JFrame {
	
	//JFrame jf2=new JFrame();
	JPanel panel2=new JPanel(null);	

	Font d = new Font("黑体", Font.BOLD, 17);
	Font f = new Font("黑体", Font.PLAIN, 15);
	JTextField searchnumber = new JTextField();
	
	JLabel intro = new JLabel("请输入您想要查询的学生学号:");
	
	JLabel infor = new JLabel("*您想要查询的学生信息如下：");
	JLabel studentsname = new JLabel("姓名：");
	JLabel studentsnumber = new JLabel("学号：");
	JLabel studentssex = new JLabel("性别：");
	JLabel studentsgrade = new JLabel("年级：");
	JLabel studentsbirthplace = new JLabel("籍贯：");
	JLabel studentsmajor = new JLabel("专业：");
	JLabel studentsschool = new JLabel("学院：");
	// public JFrame index;
	

	public StudentShowFrame() {
		init();

	}

	private void init() {
		this.setTitle("欢迎管理员查看学籍！");
		// 设置框架大小及位置

		this.setSize(550,450);
		this.setResizable(false);
		panel2.setBounds(0,0,550,450);
		
		JLabel icon = new JLabel();
		icon.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/admin.png")));
		icon.setBounds(50, 50, 30, 30);


		JLabel name = new JLabel();
		JLabel number = new JLabel();
		JLabel gender = new JLabel();
		JLabel grade = new JLabel();
		JLabel birthplace = new JLabel();
		JLabel major = new JLabel();
		JLabel school = new JLabel();
		JButton submit = new JButton();
		submit.setFocusPainted(false);
		JButton reset = new JButton("重置");
		reset.setFocusPainted(false);
		reset.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/刷新.png")));
		submit.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/search.png")));
		
		JButton del = new JButton("删除该学生");
		del.setFocusPainted(false);
		del.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/delete.png")));


		
		intro.setBounds(80, 50, 300, 30);
		intro.setFont(d);
		searchnumber.setBounds(330, 50, 80, 30);
		searchnumber.setFont(f);
		submit.setBounds(420, 50, 30, 30);
		submit.setFont(f);

		infor.setBounds(50, 90, 300, 20);
		infor.setFont(f);

		studentsname.setBounds(50, 140, 80, 20);
		studentsname.setFont(f);
		name.setBounds(130, 140, 100, 20);
		name.setFont(f);

		studentsnumber.setBounds(50, 180, 80, 20);
		studentsnumber.setFont(f);
		number.setBounds(130, 180, 100, 20);
		number.setFont(f);

		studentssex.setBounds(200, 180, 80, 20);
		studentssex.setFont(f);
		gender.setBounds(280, 180, 100, 20);
		gender.setFont(f);

		studentsgrade.setBounds(50, 220, 80, 20);
		studentsgrade.setFont(f);
		grade.setBounds(130, 220, 100, 20);
		grade.setFont(f);

		studentsbirthplace.setBounds(200, 220, 80, 20);
		studentsbirthplace.setFont(f);
		birthplace.setBounds(280, 220, 100, 20);
		birthplace.setFont(f);

		studentsmajor.setBounds(50, 260, 80, 20);
		studentsmajor.setFont(f);
		major.setBounds(130, 260, 100, 20);
		major.setFont(f);

		studentsschool.setBounds(200, 260, 80, 20);
		studentsschool.setFont(f);
		school.setBounds(280, 260, 100, 20);
		school.setFont(f);

		reset.setBounds(230, 350, 100, 25);
		reset.setFont(f);
		del.setBounds(350, 350, 150, 25);
		del.setFont(f);

		panel2.add(del);
		panel2.add(icon);
		panel2.add(intro);
		panel2.add(infor);
		panel2.add(searchnumber);
		panel2.add(studentsname);
		panel2.add(name);
		panel2.add(studentsnumber);
		panel2.add(number);
		panel2.add(studentssex);
		panel2.add(gender);
		panel2.add(grade);
		panel2.add(studentsgrade);
		panel2.add(studentsbirthplace);
		panel2.add(birthplace);
		panel2.add(studentsmajor);
		panel2.add(major);
		panel2.add(studentsschool);
		panel2.add(school);
		panel2.add(submit);
		panel2.add(reset);
		panel2.add(del);
		

		this.getContentPane().add(panel2);
		this.setVisible(true);
		// 居中显示
		this.setLocationRelativeTo(null);
		reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				searchnumber.setText("");
				name.setText("");
				number.setText("");
				gender.setText("");
				grade.setText("");
				birthplace.setText("");
				school.setText("");
				major.setText("");
			}
		});
		
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				if (searchnumber.getText().equals("") ) {
					JOptionPane.showMessageDialog(null, "请输入内容");
				}
				else {
					String snumber=searchnumber.getText();
					try
					{
						Socket socket = new Socket(IP(), 2000);
						//设置超时时间
						socket.setSoTimeout(3000);
						//构建IO
						InputStream is = socket.getInputStream();
						OutputStream os = socket.getOutputStream();
						BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
						//向服务器传输操作码以及
						bw.write("31"+'\n'+snumber+'\n');
						bw.flush();
						//建立输入输出对象流
						ObjectInputStream ois=new ObjectInputStream(is);
						//接受学籍信息对象
						Object obj = ois.readObject();
						Student student=(Student)obj;
						System.out.println(student);
						//获取信息
						name.setText(student.getName());
						gender.setText(student.getSex());
						major.setText(student.getMajor());
						grade.setText(student.getGrade());
						birthplace.setText(student.getBirthplace());
						school.setText(student.getSchool());
						number.setText(snumber);
						socket.close();
					}catch (Exception a) {
						System.out.println("连接异常！");
						JOptionPane.showMessageDialog(null, "查无此学生！");
					}
				}
			}
		});
		del.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (searchnumber.getText().equals("") ) {
					JOptionPane.showMessageDialog(null, "请输入内容");
				}
				else {
					String snumber=searchnumber.getText();
					try {
						Socket socket = new Socket(IP(), 2000);
						//设置超时时间
						socket.setSoTimeout(3000);
						//构建IO
						InputStream is = socket.getInputStream();
						OutputStream os = socket.getOutputStream();
						BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
						BufferedReader br=new BufferedReader(new InputStreamReader(is));
						//向服务器传输操作码以及
						bw.write("33"+'\n'+snumber+'\n');
						bw.flush();
						String result=br.readLine();
						System.out.println(result);
						if(Objects.equals(result, "0")){
							JOptionPane.showMessageDialog(null, "删除成功！");
						}
						if(Objects.equals(result, "1")){
							JOptionPane.showMessageDialog(null, "删除失败！*数据库异常*");
						}
						if(Objects.equals(result, "2")){
							JOptionPane.showMessageDialog(null, "删除失败！*学生退课失败*");
						}
						if(Objects.equals(result, "3")){
							JOptionPane.showMessageDialog(null, "删除失败！*学生图书未还*");
						}
						socket.close();
					}catch (Exception a){
						JOptionPane.showMessageDialog(null, "连接异常");
				}
				}
			}
		});

	}

	public static void main(String[] args) {
		new StudentShowFrame();
	}
}
